package com.asiainfo.kbisheet.control
{
	import com.asiainfo.kbisheet.common.Util;
	import com.asiainfo.kbisheet.event.AaaacashboxAutoAuthCompleteEvent;
	import com.asiainfo.kbisheet.event.AaaacashboxCreateAppRequestCompleteEvent;
	import com.asiainfo.kbisheet.event.AaaacashboxCreateResRequestCompleteEvent;
	import com.asiainfo.kbisheet.event.AaaacashboxLocalAuthCompleteEvent;
	import com.asiainfo.kbisheet.event.AaaacashboxQueryAppOperJKStatusCompleteEvent;
	import com.asiainfo.kbisheet.event.AaaacashboxQueryJKStatusByIDCompleteEvent;
	import com.asiainfo.kbisheet.event.AaaacashboxQueryResCmdJKStatusCompleteEvent;
	import com.asiainfo.kbisheet.event.AaaacashboxReSendJKPassCompleteEvent;
	import com.asiainfo.kbisheet.event.AaaacashboxRemoteAuthCompleteEvent;
	import com.asiainfo.kbisheet.event.GetDataXMLCompleteEvent;
	import com.asiainfo.kbisheet.event.GetDateDimResultEvent;
	import com.asiainfo.kbisheet.event.GetListDimResultEvent;
	import com.asiainfo.kbisheet.model.KbsModel;
	import com.asiainfo.kbisheet.model.SettingModel;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.AutoAuthQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.AutoAuthQueryResult;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.CreateAppRequestQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.CreateAppRequestQueryResult;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.CreateResRequestQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.CreateResRequestQueryResult;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.LocalAuthQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.LocalAuthQueryResult;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryAppOperJKStatusQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryAppOperJKStatusQueryResult;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryJKStatusByIDQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryJKStatusByIDQueryResult;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryResCmdJKStatusQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryResCmdJKStatusQueryResult;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.ReSendJKPassQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.ReSendJKPassQueryResult;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.RemoteAuthQueryParam;
	import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.RemoteAuthQueryResult;
	import com.asiainfo.kbisheet.vo.DimVO;
	import com.asiainfo.kbisheet.vo.FromToDateVO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	
	public class KbsControlBase extends EventDispatcher implements IKbsControl
	{
		internal var _service:RemoteObject;       		
		internal var kbsModel:KbsModel = KbsModel.getInstance();
		internal var messageControl:MessageControl = new MessageControl();
		
		public function KbsControlBase()
		{
			_service = new RemoteObject();            	
			_service.destination = "flexService";
			_service.endpoint = "messagebroker/amf";
			_service.showBusyCursor = true;
			_service.addEventListener(FaultEvent.FAULT, Util.faultHandler);
			
			_service.loadKbsSheetVO4Id.addEventListener(ResultEvent.RESULT, loadKbsSheetVO4IdResult);
			_service.loadXmlContent.addEventListener(ResultEvent.RESULT, loadXmlContentResult);
			_service.loadUserColumnSet.addEventListener(ResultEvent.RESULT, loadUserColumnSetResult);
			/*日期维度*/
			_service.getDimDate.addEventListener(ResultEvent.RESULT, getDimDateResult);
			/*下拉列表维度*/
			_service.getDimList.addEventListener(ResultEvent.RESULT, getDimListResult);
			/*报表数据*/
			_service.getDataXML.addEventListener(ResultEvent.RESULT, getDataXMLResult);
			/*创建Excel*/
			_service.createExcel.addEventListener(ResultEvent.RESULT, createExcelResult);
			
			/*4A应用资源敏感操作金库状态查询接口*/
			_service.aaaacashboxQueryAppOperJKStatus.addEventListener(ResultEvent.RESULT, aaaacashboxQueryAppOperJKStatusResult);
			/*4A系统资源敏感指令金库状态查询接口*/
			_service.aaaacashboxQueryResCmdJKStatus.addEventListener(ResultEvent.RESULT, aaaacashboxQueryResCmdJKStatusResult);
			/*4A根据金库申请id查询金库状态接口*/
			_service.aaaacashboxQueryJKStatusByID.addEventListener(ResultEvent.RESULT, aaaacashboxQueryJKStatusByIDResult);
			/*4A应用资源敏感操作金库申请创建接口*/
			_service.aaaacashboxCreateAppRequest.addEventListener(ResultEvent.RESULT, aaaacashboxCreateAppRequestResult);
			/*4A系统资源敏感指令金库申请创建接口*/
			_service.aaaacashboxCreateResRequest.addEventListener(ResultEvent.RESULT, aaaacashboxCreateResRequestResult);
			/*4A7.1	远程授权认证接口*/
			_service.aaaacashboxRemoteAuth.addEventListener(ResultEvent.RESULT, aaaacashboxRemoteAuthResult);
			/*4A7.2	现场授权认证接口*/
			_service.aaaacashboxLocalAuth.addEventListener(ResultEvent.RESULT, aaaacashboxLocalAuthResult);
			/*4A7.3	自动授权认证接口*/
			_service.aaaacashboxAutoAuth.addEventListener(ResultEvent.RESULT, aaaacashboxAutoAuthResult);
			/*4A8	金库口令重发接口*/
			_service.aaaacashboxReSendJKPass.addEventListener(ResultEvent.RESULT, aaaacashboxReSendJKPassResult);
		}
		
		
		public function getDataXML():void{
			_service.getDataXML(kbsModel.queryVO);
		}
		
		/**
		 * 5.1	应用资源敏感操作金库状态查询接口
		 */
		public function aaaacashboxQueryAppOperJKStatus(param:QueryAppOperJKStatusQueryParam):void{
			_service.aaaacashboxQueryAppOperJKStatus(param);
		}

		private function aaaacashboxQueryAppOperJKStatusResult(event:ResultEvent) : void{
			var result:QueryAppOperJKStatusQueryResult = _service.aaaacashboxQueryAppOperJKStatus.lastResult;
			var aaaacashboxQueryAppOperJKStatusCompleteEvent:AaaacashboxQueryAppOperJKStatusCompleteEvent = new AaaacashboxQueryAppOperJKStatusCompleteEvent(result);
			dispatchEvent(aaaacashboxQueryAppOperJKStatusCompleteEvent);
			Alert.show("aaaacashboxQueryAppOperJKStatusResult!");
		}
		
		/**
		 * 5.2	系统资源敏感指令金库状态查询接口
		 */
		public function aaaacashboxQueryResCmdJKStatus(param:QueryResCmdJKStatusQueryParam):void{
			_service.aaaacashboxQueryResCmdJKStatus(param);
		}
		
		private function aaaacashboxQueryResCmdJKStatusResult(event:ResultEvent) : void{
			var result:QueryResCmdJKStatusQueryResult = _service.aaaacashboxQueryResCmdJKStatusQueryResult.lastResult;
			var aaaacashboxQueryResCmdJKStatusCompleteEvent:AaaacashboxQueryResCmdJKStatusCompleteEvent = new AaaacashboxQueryResCmdJKStatusCompleteEvent(result);
			dispatchEvent(aaaacashboxQueryResCmdJKStatusCompleteEvent);
		}
		/**
		 * 5.3	根据金库申请id查询金库状态接口
		 */
		public function aaaacashboxQueryJKStatusByID(param:QueryJKStatusByIDQueryParam):void{
			_service.aaaacashboxQueryJKStatusByID(param);
		}
		
		private function aaaacashboxQueryJKStatusByIDResult(event:ResultEvent) : void{
			var result:QueryJKStatusByIDQueryResult = _service.aaaacashboxQueryJKStatusByIDQueryResult.lastResult;
			var aaaacashboxQueryJKStatusByIDCompleteEvent:AaaacashboxQueryJKStatusByIDCompleteEvent = new AaaacashboxQueryJKStatusByIDCompleteEvent(result);
			dispatchEvent(aaaacashboxQueryJKStatusByIDCompleteEvent);
		}
		
		/**
		 * 6.1	应用资源敏感操作金库申请创建接口
		 */
		public function aaaacashboxCreateAppRequest(param:CreateAppRequestQueryParam):void{
			_service.aaaacashboxCreateAppRequest(param);
		}
		
		private function aaaacashboxCreateAppRequestResult(event:ResultEvent) : void{
			var result:CreateAppRequestQueryResult = _service.aaaacashboxCreateAppRequestQueryResult.lastResult;
			var aaaacashboxCreateAppRequestCompleteEvent:AaaacashboxCreateAppRequestCompleteEvent = new AaaacashboxCreateAppRequestCompleteEvent(result);
			dispatchEvent(aaaacashboxCreateAppRequestCompleteEvent);
		}
		/**
		 * 6.2	系统资源敏感指令金库申请创建接口
		 */
		public function aaaacashboxCreateResRequest(param:CreateResRequestQueryParam):void{
			_service.aaaacashboxCreateResRequest(param);
		}
		
		private function aaaacashboxCreateResRequestResult(event:ResultEvent) : void{
			var result:CreateResRequestQueryResult = _service.aaaacashboxCreateResRequestQueryResult.lastResult;
			var aaaacashboxCreateResRequestCompleteEvent:AaaacashboxCreateResRequestCompleteEvent = new AaaacashboxCreateResRequestCompleteEvent(result);
			dispatchEvent(aaaacashboxCreateResRequestCompleteEvent);
		}
		
		/**
		 * 7.1	远程授权认证接口
		 */
		public function aaaacashboxRemoteAuth(param:RemoteAuthQueryParam):void{
			_service.aaaacashboxRemoteAuth(param);
		}
		
		private function aaaacashboxRemoteAuthResult(event:ResultEvent) : void{
			var result:RemoteAuthQueryResult = _service.aaaacashboxRemoteAuthQueryResult.lastResult;
			var aaaacashboxRemoteAuthCompleteEvent:AaaacashboxRemoteAuthCompleteEvent = new AaaacashboxRemoteAuthCompleteEvent(result);
			dispatchEvent(aaaacashboxRemoteAuthCompleteEvent);
		}
		
		/**
		 * 7.2	现场授权认证接口
		 */
		public function aaaacashboxLocalAuth(param:LocalAuthQueryParam):void{
			_service.aaaacashboxLocalAuth(param);
		}
		
		private function aaaacashboxLocalAuthResult(event:ResultEvent) : void{
			var result:LocalAuthQueryResult = _service.aaaacashboxLocalAuthQueryResult.lastResult;
			var aaaacashboxLocalAuthCompleteEvent:AaaacashboxLocalAuthCompleteEvent = new AaaacashboxLocalAuthCompleteEvent(result);
			dispatchEvent(aaaacashboxLocalAuthCompleteEvent);
		}
		
		/**
		 * 7.3	自动授权认证接口
		 */
		public function aaaacashboxAutoAuth(param:AutoAuthQueryParam):void{
			_service.aaaacashboxAutoAuth(param);
		}
		
		private function aaaacashboxAutoAuthResult(event:ResultEvent) : void{
			var result:AutoAuthQueryResult = _service.aaaacashboxAutoAuthQueryResult.lastResult;
			var aaaacashboxAutoAuthCompleteEvent:AaaacashboxAutoAuthCompleteEvent = new AaaacashboxAutoAuthCompleteEvent(result);
			dispatchEvent(aaaacashboxAutoAuthCompleteEvent);
		}
		
		/**
		 * 8	金库口令重发接口
		 */
		public function aaaacashboxReSendJKPass(param:ReSendJKPassQueryParam):void{
			_service.aaaacashboxReSendJKPass(param);
		}
		
		private function aaaacashboxReSendJKPassResult(event:ResultEvent) : void{
			var result:ReSendJKPassQueryResult = _service.aaaacashboxReSendJKPassQueryResult.lastResult;
			var aaaacashboxReSendJKPassCompleteEvent:AaaacashboxReSendJKPassCompleteEvent = new AaaacashboxReSendJKPassCompleteEvent(result);
			dispatchEvent(aaaacashboxReSendJKPassCompleteEvent);
		}
		
		
		public function getUserInfo():void{
			/*直接分发完成事件，实现类需要重载该方法*/
			var newEvent:Event = new Event("getUserInfoComplete");
			dispatchEvent(newEvent);
		}
		
		public function loadDefineXml():void{
			/*根据报表ID得到KbsSheetVO对象*/
			kbsModel.sid = FlexGlobals.topLevelApplication.parameters["sid"];
			
			updateMessage("KbsControlBase.loadDefineXml():得到报表sid:"+kbsModel.sid);
			if(kbsModel.sid == null){
				Alert.show("未指定报表ID！","参数错误");
				return;
			}
			_service.loadKbsSheetVO4Id(kbsModel.sid);
		}
		
		public function getDimList(dimVO:DimVO):void {
			_service.getDimList(dimVO);
		}
		
		public function getDimDate(dimVO:DimVO):void {
			_service.getDimDate(dimVO);
		}
		
		public function createExcel(xmlString:String):void {
			_service.createExcel(xmlString);
		}
		
		protected function decrypt(key:String, src:String):void{
			updateMessage("KbsControlBase：decrypt：解码字符串...");
			_service.decrypt(key, src);
		}
		
		/* 更新消息 */
		protected function updateMessage(message:String):void{
			messageControl.updateMessage(message);
		}
		
		private function loadKbsSheetVO4IdResult(event:ResultEvent):void{
			kbsModel.kbsSheetVO = _service.loadKbsSheetVO4Id.lastResult;
			if(kbsModel.kbsSheetVO !=null){
				updateMessage("报表标题:" + kbsModel.kbsSheetVO.sheetTitle + "报表文件名:" + kbsModel.kbsSheetVO.xmlName + " ;xml更新时间:" + kbsModel.kbsSheetVO.xmlTime);
				updateMessage("文件存放路径:" + SettingModel.getInstance().settingVO.filesRoot);
				_service.loadXmlContent(kbsModel.kbsSheetVO.xmlName);
			}
			else{
				Alert.show("指定报表ID[ " + kbsModel.sid + " ]不存在！" ,"参数错误");
			}
		}	
		
		/*加载XML内容的结果*/
		private function loadXmlContentResult(event:ResultEvent):void{			
			kbsModel.sheetXML = new XML(_service.loadXmlContent.lastResult);
			if(SettingModel.getInstance().settingVO.debug_xml){
				updateMessage("KbsControlBase.loadXmlContentResult():XML内容:\r\n" + kbsModel.sheetXML);
			}
			_service.loadUserColumnSet(kbsModel.userId,kbsModel.sid);
		}
		
		/*加载用户列设置的结果*/
		private function loadUserColumnSetResult(event:ResultEvent):void{
			kbsModel.hiddenColumnUUIDs = _service.loadUserColumnSet.lastResult;
			var newEvent:Event = new Event("loadDefineXmlComplete");
			dispatchEvent(newEvent);
		}
		
		
		/*加载表格数据的结果*/
		private function getDataXMLResult(event:ResultEvent):void{
			var dataXML:XML = new XML(_service.getDataXML.lastResult);
			if(SettingModel.getInstance().settingVO.debug_xml){
				updateMessage("获取表格数据XML:" + dataXML);
			}
			kbsModel.maxCount = int(dataXML.@maxCount);
			kbsModel.maxPage = (kbsModel.maxCount%kbsModel.countPerPage)==0 ? kbsModel.maxCount/kbsModel.countPerPage : kbsModel.maxCount/kbsModel.countPerPage + 1
			var newEvent:Event = new GetDataXMLCompleteEvent(dataXML);
			dispatchEvent(newEvent);
		}	
		
		
		
		/*得到時間維表的值的結果*/
		private function getDimDateResult(event:ResultEvent):void{
			var fromToDateVO:FromToDateVO = _service.getDimDate.lastResult;
			var selectableRange:Object = null;
			/*得到時間維表的結果*/
			if(fromToDateVO){
				updateMessage("维表范围：fromDate:" + fromToDateVO.fromDate+ ";toDate:" + fromToDateVO.toDate);
				var fromDate:Date;
				var toDate:Date;
				if(fromToDateVO.fromDate.length == 6){
					/*月格式*/
					fromDate = string2Month(fromToDateVO.fromDate);
					toDate = string2Month(fromToDateVO.toDate);
				}
				else{
					/*日格式*/
					fromDate = string2Date(fromToDateVO.fromDate);
					toDate = string2Date(fromToDateVO.toDate);
				}
				updateMessage("日期范围：fromDate:" + fromDate+ ";toDate:" + toDate);
				selectableRange = {rangeStart:fromDate,rangeEnd:toDate};
			}
			var getDateDimResultEvent:GetDateDimResultEvent = new GetDateDimResultEvent(selectableRange);
			dispatchEvent(getDateDimResultEvent);
		}
		
		private function getDimListResult(event:ResultEvent):void{
			var dataList:ArrayCollection = _service.getDimList.lastResult;
			var getListDimResultEvent:GetListDimResultEvent = new GetListDimResultEvent(dataList);
			dispatchEvent(getListDimResultEvent);
		}
		
		private function createExcelResult(event:ResultEvent):void{
			updateMessage("KbsControlBase.createExcelResult():创建Excel内容完成");
			ExternalInterface.call("loadExcelFile", String(_service.createExcel.lastResult));
		}
		
		/*把字符串转为日期*/
		private function string2Date(value:String,offset:int=0):Date{
			var year:int =	int(value.substr(0,4));
			var month:int =	int(value.substr(4,2)) - 1;
			var day:int = int(value.substr(6,2)) + offset;
			var date:Date = new Date(year,month,day);
			return date;
		}
		
		/*把字符串转为月的日期*/
		private function string2Month(value:String):Date{
			var year:int =	int(value.substr(0,4));
			var month:int =	int(value.substr(4,2)) - 1;
			var date:Date = new Date(year,month);
			return date;
		}
	}
}